﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects.DataClasses;
using BL.Membership;
using System.Linq.Expressions;

namespace DA
{
    public abstract class OrderDeskEntityRepository<TEntity> : BaseLib.Data.BaseEntityRepository<TEntity, OrdersDeskEntities>
        where TEntity : EntityObject
    {
        private bool _filterByClient = true;
        protected UserData UserData { get; private set; }

        public OrderDeskEntityRepository()
        {
            UserData = new OrderDeskMembership().GetCurrentLoggedInUser();
        }

        public void SetClientFiltering(bool filterByClient)
        {
            _filterByClient = filterByClient;
        }

        public override IQueryable<TEntity> GetEntitySet(params string[] includes)
        {
            var query = base.GetEntitySet(includes);

            if (_filterByClient && UserData != null && !UserData.IsUserAllowed(UserPermission.AllClientsAdmin))
                query = query.Where(FilterByClientId(UserData.ClientId));

            return query;
        }

        protected virtual Expression<Func<TEntity, bool>> FilterByClientId(int clientId)
        {
            return x => true;
        }
    }
}
